my @D;
my $i;
for($i=1;$i<=5;$i++){
    $D[0][$i][$i] = 0 ; 
}
$D[0][1][2] =  3;
$D[0][1][3] =  8;
$D[0][1][4] =  1000;
$D[0][1][5] =  -4;

$D[0][2][1] =  1000;
$D[0][2][3] =  1000;
$D[0][2][4] =  1;
$D[0][2][5] =  7;

$D[0][3][1] =  1000;
$D[0][3][2] =  4;
$D[0][3][4] =  1000;
$D[0][3][5] =  1000;

$D[0][4][1] =  -3; # 2 
$D[0][4][2] =  1000;
$D[0][4][3] =  -5; # -5 
$D[0][4][5] =  1000;

$D[0][5][1] =  1000;
$D[0][5][2] =  1000;
$D[0][5][3] =  1000;
$D[0][5][4] =  6;

my $n=5;
my $k;
print_matrix(0);
for($k=1;$k <= 5; $k++){
    for($i=1;$i <= 5; $i++){
        for($j=1;$j <= 5; $j++){
            $D[$k][$i][$j] = min($D[$k-1][$i][$j],$D[$k-1][$i][$k] + $D[$k-1][$k][$j]);
        }
    }
    print_matrix($k);
}


sub min {
        my $a = shift @_;
        my $b = shift @_;
        if($a > $b ) {
            return $b;
        }else{
            return $a;
    }
        
}
sub print_matrix {
    my $k = shift @_;
    my $i;
    my $j;
    print "The $k th Matrix is \n";
    for($i=1;$i<=5;$i++){
        for($j=1;$j<=5;$j++){
            print "\t$D[$k][$i][$j]";
        }
        print "\n";
    }

}


